﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Abp.Runtime.Session;
using AVISOutBound.SSOServer.Setting.Dto;
using MyCompanyName.AbpZeroTemplate.Configuration;

namespace AVISOutBound.SSOServer.Setting
{
    public class SettingAppService : SSOServerAppServiceBase, ISettingAppService
    {
        public async Task<AliPaySettingsEditDto> GetAllAliPaySettings()
        {
            return new AliPaySettingsEditDto
            {
                Url = await SettingManager.GetSettingValueAsync(AppSettings.PaymentConfig.Alipay.Url),
                Appid = await SettingManager.GetSettingValueAsync(AppSettings.PaymentConfig.Alipay.Appid),
                PrivateKey = await SettingManager.GetSettingValueAsync(AppSettings.PaymentConfig.Alipay.PrivateKey),
                PublicKey = await SettingManager.GetSettingValueAsync(AppSettings.PaymentConfig.Alipay.PublicKey),
                Ppid = await SettingManager.GetSettingValueAsync(AppSettings.PaymentConfig.Alipay.Ppid),
                Format = await SettingManager.GetSettingValueAsync(AppSettings.PaymentConfig.Alipay.Format),
                Charset = await SettingManager.GetSettingValueAsync(AppSettings.PaymentConfig.Alipay.Charset),
                Sign_type = await SettingManager.GetSettingValueAsync(AppSettings.PaymentConfig.Alipay.Sign_type),
                Version = await SettingManager.GetSettingValueAsync(AppSettings.PaymentConfig.Alipay.Version),
            };
        }

        public async Task UpdateAliPaySettings(AliPaySettingsEditDto input)
        {
            await SettingManager.ChangeSettingForApplicationAsync( AppSettings.PaymentConfig.Alipay.Url, input.Url);
            await SettingManager.ChangeSettingForApplicationAsync(AppSettings.PaymentConfig.Alipay.Appid, input.Appid);
            await SettingManager.ChangeSettingForApplicationAsync( AppSettings.PaymentConfig.Alipay.PrivateKey, input.PrivateKey);
            await SettingManager.ChangeSettingForApplicationAsync(AppSettings.PaymentConfig.Alipay.PublicKey, input.PublicKey);
            await SettingManager.ChangeSettingForApplicationAsync(AppSettings.PaymentConfig.Alipay.Ppid, input.Ppid);
            await SettingManager.ChangeSettingForApplicationAsync(AppSettings.PaymentConfig.Alipay.Format, input.Format);
            await SettingManager.ChangeSettingForApplicationAsync(AppSettings.PaymentConfig.Alipay.Charset, input.Charset);
            await SettingManager.ChangeSettingForApplicationAsync(AppSettings.PaymentConfig.Alipay.Sign_type, input.Sign_type);
            await SettingManager.ChangeSettingForApplicationAsync(AppSettings.PaymentConfig.Alipay.Version, input.Version);
        }

        public async Task<WechatPaySettingsEditDto> GetAllWechatPaySettings()
        {
            return new WechatPaySettingsEditDto()
            {
                AppId = await SettingManager.GetSettingValueAsync(AppSettings.PaymentConfig.Wechat.AppId),
                MchId = await SettingManager.GetSettingValueAsync(AppSettings.PaymentConfig.Wechat.MchId),
                CertPath = await SettingManager.GetSettingValueAsync(AppSettings.PaymentConfig.Wechat.CertPath),
                Password = await SettingManager.GetSettingValueAsync(AppSettings.PaymentConfig.Wechat.Password),
                PayKey = await SettingManager.GetSettingValueAsync(AppSettings.PaymentConfig.Wechat.PayKey)
            };
        }

        public async Task UpdateWechatPaySettings(WechatPaySettingsEditDto input)
        {
            await SettingManager.ChangeSettingForApplicationAsync(AppSettings.PaymentConfig.Wechat.AppId, input.AppId);
            await SettingManager.ChangeSettingForApplicationAsync(AppSettings.PaymentConfig.Wechat.MchId, input.MchId);
            await SettingManager.ChangeSettingForApplicationAsync(AppSettings.PaymentConfig.Wechat.CertPath, input.CertPath);
            await SettingManager.ChangeSettingForApplicationAsync(AppSettings.PaymentConfig.Wechat.Password, input.Password);
            await SettingManager.ChangeSettingForApplicationAsync(AppSettings.PaymentConfig.Wechat.PayKey, input.PayKey);
        }
    }
}
